define(function (require, exports, module) {
    require("resource/js/easyui/themes/icon.css");
    require("resource/js/easyui/easyloader.js");
    require.async("resource/js/component/rules.js");
    var AJAX = require("resource/js/component/ajax.js");
    var A = require("resource/js/component/ajax.js");
    var C = require("resource/js/config/const.js");
    var easyutil = require("resource/js/component/easyutil.js");
    var easycombobox = require("resource/js/component/easycombobox.js");
    easycombobox.create({
		url:BASEPATH+"/base/authoriza/basecode/getType.htm",
		param:{checkeds:"isCurrent"}
	})

    var push = undefined;
    //校验唯一性
    using(["validatebox"],function(){
        $.extend($.fn.validatebox.defaults.rules, {
            //校验工艺ID 和搅拌机编号是否存在记录
            machineNoResulAdd:{
                validator: function(value){
                    var layerId = $("#layerId_add").combobox("getValue");
                    var machineNo = $('#machineNo_add').val();
                    //标段ID
                    var sectionId = "";
                    var bol = false;
                    if(layerId != null  && machineNo != null){
                        AJAX.init().sync().success(function(code,msg,data){
                            bol = data == "0" ? true : false;
                        }).error(function(code,msg,data){
                        }).post(BASEPATH+'/dcs/water/blender/queryWaterLayerIdMachineNo.htm',{"layerId":layerId,"machineNo":machineNo,"stationId":stationId});
                    }
                    return bol;
                },message: "存在重复编号对应工艺"
            },
            //校验工艺ID 和搅拌机编号是否存在记录
            machineNoResulUpd:{
                validator: function(value){
                    var layerId = $("#layerId_upd").combobox("getValue");
                    var machineNo = $('#machineNo_upd').val();
                    //标段ID
                    var sectionId = "";
                    var bol = false;
                    if(push.machineNo == value && push.layerId == layerId){
                        bol = true;
                    }else{
                        if(layerId != null  && machineNo != null){
                            AJAX.init().sync().success(function(code,msg,data){
                                bol = data == "0" ? true : false;
                            }).error(function(code,msg,data){
                            }).post(BASEPATH+'/dcs/water/blender/queryWaterLayerIdMachineNo.htm',{"layerId":layerId,"machineNo":machineNo,"stationId":stationId});
                        }
                    }
                    return bol;
                },message: "存在重复编号对应工艺"
            }
        })
    })

    var config = {
        name: "拌合参数设置",
        column: [[
            {field: 'waterBlenderId', title: '控制机ID', width: 100, align: 'center', height: 50, hidden: true},
            {field: 'deviceName', title: '控制机名称', width: 100, align: 'center', height: 50},
            {field: 'codeNum', title: '编码', width: 100, align: 'center', height: 50},
            {field: 'layer_id', title: '结构层ID', width: 100, align: 'center', height: 50, height: 50, hidden: true},
            {field: 'layerName', title: '结构层', width: 100, align: 'center', height: 50},
            {field: 'material_id', title: '混料类型ID', width: 100, align: 'center', height: 50, height: 50, hidden: true},
            {field: 'materialName', title: '混料类型', width: 100, align: 'center', height: 50},
            {field:'isCurrent',title:'是否上报',width:100,align:'center',height:50,formatter:function(value,row,index){
                    if(value=="1"){
                        return "<span style='color: red;cursor: pointer;'><input type='checkbox'  checked  class='isCurrent tab_style' value='0'  layerId='"+row.layerId+"' machineNo = '"+row.machineNo+"'  status='"+value+"' id='"+row.waterBlenderId+"' ></input></span>";
                    }else if(value=="0"){
                        return "<span style='color: blue;cursor: pointer;'><input type='checkbox'  class='isCurrent tab_style' value='1' layerId='"+row.layerId+"' machineNo = '"+row.machineNo+"'  status='"+value+"' id='"+row.waterBlenderId+"' ></input></span>";
                    }
	        }}
        ]
        ]
    }


    var dgutil = undefined;
    var datagridconfig = undefined;
    var stationId = "",projectNo="",sectionNo="",stationNo="";
    function initPage() {
    	stationId = $("#stationId_search").val();
		projectNo = $("#projectNo_search").val();
		sectionNo = $("#sectionNo_search").val();
		stationNo = $("#stationNo_search").val();
        dgutil = new easyutil.ajaxDataGrid(datagridconfig);
    }

    function initLayerAndMaterial(pid, cid, row) {
        /**
         * 一级
         */
        A.init().sync().success(function (code, msg, data) {
            easycombobox.init({
                ele: "#" + pid,
                valueField: 'id',
                remote: false,
                textField: 'name',
                prompt: '请选择',
                onChange: function (rec) {
                    $("#" + cid).combobox("clear");
                    A.init().sync().success(function (code, msg, data) {
                        easycombobox.init({
                            ele: "#" + cid,
                            valueField: 'id',
                            remote: false,
                            textField: 'name',
                            prompt: '请选择'

                        }).load(data);
                    }).post(BASEPATH + '/base/authoriza/conmonData/queryDataNoCache.htm', {
                        queryId: 'getDcsMaterial',
                        param: $("#" + pid).combobox("getValue")
                    });
                }
            }).load(data);
        }).error(function (code, msg, data) {
            $.messager.alert("系统提示", msg, "error");
        }).post(BASEPATH + '/base/authoriza/conmonData/queryDataNoCache.htm', {"queryId": "getDcsLayer"});

        /**
         * 二级
         */
        A.init().sync().success(function (code, msg, data) {
            easycombobox.init({
                ele: "#" + cid,
                valueField: 'id',
                remote: false,
                textField: 'name',
                prompt: '请选择',
                onChange: function (rec) {

                }
            }).load(data);
        }).post(BASEPATH + '/base/authoriza/conmonData/queryDataNoCache.htm', {
            queryId: 'baseareaparam',
            param: $("#" + pid).combobox("getValue")
        });
        if (row && row.layerId) {
            $("#" + pid).combobox("setValue", row.layerId);
        }
        if (row && row.materialId) {
            $("#" + cid).combobox("setValue", row.materialId);
        }
    }
    
    var getRandom = function(){
		var hostName = window.location.hostname;
		if(hostName == 'localhost'){
			hostName = '127.0.0.1';
		}
		return hostName.replace(/\./g,'')+(new Date().getTime())+Math.round(Math.random()*10000);
	}
    
    //拌合参数添加
    var addDialogInit = function (baseUrl, row) {
        $("#adddlg").dialog({
            iconCls: "icon-save",
            title: "增加",
            href: baseUrl,
            onLoad: function () {
            	easycombobox.init({ ele:"#isCurrent_add", grouptype:"isCurrent"});
                $("#waterStationId_add").val($("#waterStationId").val());
                initLayerAndMaterial('layerId_add', 'materialId_add');
                $("#token_id_add").val(getRandom());
            },
            buttons: [{
                text: "保存",
                iconCls: "icon-ok",
                accessKey: {akey: 18, mkey: 83},
                handler: function () {
                    using("form", function () {
                        if ($("#addparamform").form("validate")) {
                            if ($("#addparamform1").form("validate")) {
                                /*if(!checkDate('_Add')){
                                    return;
                                }*/
                                /*var obj1 = $("#addform1").serializeJson();*/
                                var obj = $("#addparamform").serializeJson();
                                obj.stationId = stationId;
                                obj.codeNum = projectNo+"-"+sectionNo+"-"+stationNo+"-"+obj.machineNo;
                                var arr = ['0008', '01', '02', '05', '1', '2', '5', '10', '20', '40', '50'];
                                var arr_obj = [];
                                for (var i = 0; i < arr.length; i++) {
                                    var o_ = {};
                                    o_.poreType = $('#' + arr[i] + 'poreType_add').val();
                                    o_.designValue = $('#' + arr[i] + 'designValue_add').val();
                                    o_.workMin = $('#' + arr[i] + 'workMin_add').val();
                                    o_.workMax = $('#' + arr[i] + 'workMax_add').val();
                                    o_.designMin = $('#' + arr[i] + 'designMin_add').val();
                                    o_.designMax = $('#' + arr[i] + 'designMax_add').val();
                                    o_.aggregate1 = $('#' + arr[i] + 'aggregate1_add').val();
                                    o_.aggregate2 = $('#' + arr[i] + 'aggregate2_add').val();
                                    o_.aggregate3 = $('#' + arr[i] + 'aggregate3_add').val();
                                    o_.aggregate4 = $('#' + arr[i] + 'aggregate4_add').val();
                                    o_.aggregate5 = $('#' + arr[i] + 'aggregate5_add').val();
                                    o_.aggregate6 = $('#' + arr[i] + 'aggregate6_add').val();
                                    arr_obj.push(o_);
                                }
                                var arr_obj_json = JSON.stringify(arr_obj);
                                obj.arr_obj_json = arr_obj_json;
                                
                                /*easyutil.ajaxHandler(BASEPATH + "/dcs/water/blender/saveWaterBlenderModel.htm", obj, function () {
                                    dgutil.pager();
                                    $("#adddlg").dialog("close");
                                });*/
                                $.ajax({
									type: 'POST',
									url: BASEPATH+"/dcs/water/blender/saveWaterBlenderModel.htm",
									data: obj,
									dataType: 'json',
									success: function (data) {
										dgutil.pager();
										if(data.msg.length > 0){
											$.messager.show({title:C.MESSAGE.MSGTITLE,msg:data.msg});
										}
										$("#adddlg").dialog("close");
									}
								});
                                
                            }
                        }
                    });
                }
            },
                {
                    text: '取消',
                    addClass: 'l-btn-red', iconCls: 'icon-not',
                    accessKey: {akey: 18, mkey: 67},
                    handler: function () {
                        $("#adddlg").dialog("close");
                    }
                }
            ]
        });
        $("#adddlg").dialog("open");
    }

    //拌合参数修改
    var updDialogInit = function (baseUrl, row) {
        $("#adddlg").dialog({
            iconCls: "icon-save",
            title: "修改",
            href: baseUrl,
            onLoad: function () {
                push = row;
            	easycombobox.init({ ele:"#isCurrent_upd", grouptype:"isCurrent"});
                $("#waterStationId_upd").val($("#waterStationId").val());
                initLayerAndMaterial('layerId_upd', 'materialId_upd');
                AJAX.init().sync().success(function (code, msg, data) {
                    $('#updparamform').form('load', data);
                }).post(BASEPATH + '/dcs/water/blender/queryWaterBlenderById.htm', {waterBlenderId: row.waterBlenderId});
                AJAX.init().sync().success(function (code, msg, data) {
                    $('#updparamform1').form('load', data);
                    var top = ['0008', '01', '02', '05', '1', '2', '5', '10', '20', '40', '50'];
                    for (var i = 0; i < top.length; i++) {
                        $('#' + top[i] + 'waterPoreId_upd').val(data[i].waterPoreId);
                        $('#' + top[i] + 'poreType_upd').val(data[i].poreType);
                        $('#' + top[i] + 'designValue_upd').val( data[i].designValue);
                        $('#' + top[i] + 'workMin_upd').val( data[i].workMin);
                        $('#' + top[i] + 'workMax_upd').val( data[i].workMax);
                        $('#' + top[i] + 'designMin_upd').val(data[i].designMin);
                        $('#' + top[i] + 'designMax_upd').val( data[i].designMax);
                        $('#' + top[i] + 'aggregate1_upd').val( data[i].aggregate1);
                        $('#' + top[i] + 'aggregate2_upd').val( data[i].aggregate2);
                        $('#' + top[i] + 'aggregate3_upd').val( data[i].aggregate3);
                        $('#' + top[i] + 'aggregate4_upd').val( data[i].aggregate4);
                        $('#' + top[i] + 'aggregate5_upd').val( data[i].aggregate5);
                        $('#' + top[i] + 'aggregate6_upd').val( data[i].aggregate6);
                    }
                }).post(BASEPATH + '/dcs/water/pore/queryWaterPoreByBlenderId.htm', {waterBlenderId: row.waterBlenderId});
                
                $('#token_id_upd').val(getRandom());
                
            },
            buttons: [{
                text: "保存",
                iconCls: "icon-ok",
                accessKey: {akey: 18, mkey: 83},
                handler: function () {
                    using("form", function () {
                        if ($("#updparamform").form("validate")) {
                            if ($("#updparamform1").form("validate")) {
                                var obj = $("#updparamform").serializeJson();
                                obj.stationId = stationId;
                                obj.codeNum = projectNo+"-"+sectionNo+"-"+stationNo+"-"+obj.machineNo;
                                var arr = ['0008', '01', '02', '05', '1', '2', '5', '10', '20', '40', '50'];
                                var arr_obj = [];
                                for (var i = 0; i < arr.length; i++) {
                                    var o_ = {};
                                    o_.waterPoreId = $('#' + arr[i] + 'waterPoreId_upd').val();
                                    o_.poreType = $('#' + arr[i] + 'poreType_upd').val();
                                    o_.designValue = $('#' + arr[i] + 'designValue_upd').val();
                                    o_.workMin = $('#' + arr[i] + 'workMin_upd').val();
                                    o_.workMax = $('#' + arr[i] + 'workMax_upd').val();
                                    o_.designMin = $('#' + arr[i] + 'designMin_upd').val();
                                    o_.designMax = $('#' + arr[i] + 'designMax_upd').val();
                                    o_.aggregate1 = $('#' + arr[i] + 'aggregate1_upd').val();
                                    o_.aggregate2 = $('#' + arr[i] + 'aggregate2_upd').val();
                                    o_.aggregate3 = $('#' + arr[i] + 'aggregate3_upd').val();
                                    o_.aggregate4 = $('#' + arr[i] + 'aggregate4_upd').val();
                                    o_.aggregate5 = $('#' + arr[i] + 'aggregate5_upd').val();
                                    o_.aggregate6 = $('#' + arr[i] + 'aggregate6_upd').val();
                                    arr_obj.push(o_);
                                }
                                var arr_obj_json = JSON.stringify(arr_obj);
                                obj.arr_obj_json = arr_obj_json;
                                /*easyutil.ajaxHandler(BASEPATH + "/dcs/water/blender/updateWaterBlender.htm", obj, function () {
                                    dgutil.pager();
                                    $("#adddlg").dialog("close");
                                });*/
                                
                                $.ajax({
									type: 'POST',
									url: BASEPATH+"/dcs/water/blender/updateWaterBlender.htm",
									data: obj,
									dataType: 'json',
									success: function (data) {
										dgutil.pager();
										if(data.msg.length > 0){
											$.messager.show({title:C.MESSAGE.MSGTITLE,msg:data.msg});
										}
										$("#adddlg").dialog("close");
									}
								});
                                
                            }
                        }
                    });
                }
            },
                {
                    text: '取消',
                    addClass: 'l-btn-red', iconCls: 'icon-not',
                    accessKey: {akey: 18, mkey: 67},
                    handler: function () {
                        $("#adddlg").dialog("close");
                    }
                }
            ]
        });
        $("#adddlg").dialog("open");
    }

    //删除拌合站
    var delInit = function (baseUrl, row) {
        easyutil.ajaxHandler(baseUrl, row, function () {
            initPage();
        });
    }

    var isCurrent = function(target){
		AJAX.init().success(function(code,msg,data){
			dgutil.pager();
		}).error(function(code,msg,data){
		}).post(BASEPATH+'/dcs/water/blender/updateIsCurrent.htm',{"machineNo":$(target).attr("machineNo"),"layerId":$(target).attr("layerId"),"isCurrent":$(target).attr("value"),"stationId":stationId});
	}

    exports.trans = function () {
        var buttons = {
            title: {
                _class: "",
                text: "拌和参数管理"

            },
            btn: [
//                  {
//                iconCls: "icon-search",
//                text: "查询",
//                addClass: "l-btn-display",
//                url: "/dcs/water/blender/queryBlenderByPage.htm",
//                handler: function () {
//                    dgutil.pager(1);
//                }
//            },
                {
                    iconCls: "icon-add",
                    text: "添加",
                    /*addClass:"l-btn-display",*/
                    url: "/view/dcs/water/device/add",
                    handler: addDialogInit
                },
                {
                    iconCls: "icon-edit",
                    text: "修改",
                    /*addClass:"l-btn-display",*/
                    url: "/view/dcs/water/device/upd",
                    needRow: true,
                    handler: updDialogInit
                },
                {
                    iconCls: 'icon-remove',
                    url: '/dcs/water/blender/delBlenderModel.htm',
                    text: "删除",
                    /*addClass:'l-btn-display',*/
                    needRow: true,
                    isCheck: true,
                    handler: delInit
                }
            ]
        };
        datagridconfig = {
            fitColumns: true,
            singleSelect: false,
            striped: true,
            autoRowHeight: false,
            fit: true,
            url: BASEPATH + "/dcs/water/blender/queryBlenderByPage.htm",
            toolbar: buttons,
            columns: config.column,
            eid: "dg1",
            args: "searchform",
			onLoadSuccess:function(data){
				$(".isCurrent").bind('click',function(){
					isCurrent(this);
				});
			}
        };
        using(["datagrid", "parser", "textbox", "numberbox", "dialog", "combobox", "datetimebox", "validatebox", "messager"], function () {
            initPage();
            //按钮返回
            $("#backBtn").on("click", function () {
                $("#paramdlg").dialog("close");
            });
        });
    }
});


